package com.hugy.test.controller;

import com.hugy.test.model.TestUser;
import com.hugy.test.model.UserDTO;
import com.hugy.test.service.MongoDBTestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/mongodb")
public class MongoDBTestController {

	@Resource
	private MongoDBTestService mongoDBTestService;
	
	//增操作
	@PostMapping(value = "/add")
	public String addMongoDB() {
		UserDTO user = new UserDTO("123",23);
  		mongoDBTestService.addMongoDB(user);
  		return "SUCCESS";
	}

	//增操作
	@PostMapping(value = "/add2")
	public String addMongoDB2() {
		TestUser user = new TestUser("123",23);
		mongoDBTestService.addMongoDB(user);
		return "SUCCESS";
	}

	@GetMapping(value = "/delete/{userName}")
	public String deleteMongoDB(@PathVariable(value = "userName") String userName) {
		mongoDBTestService.deleteMongoDB(userName);
		return "SUCCESS";
	}

	//更改一条数据的一个字段
	@PostMapping(value = "/update")
	public String updateMongoDB() {
		TestUser user = new TestUser("123",18);
		mongoDBTestService.updateMongoDB(user);
		return "SUCCESS";
	}

	//更改一条数据的多个字段
	@PostMapping(value = "/update/many")
	public String updateManyMongoDB() {
		TestUser user = new TestUser("666",21);
		mongoDBTestService.updateManyMongoDB(user);
		return "SUCCESS";
	}

	//查询一条数据
	@GetMapping(value = "/find/one/{userName}")
	public TestUser getOneMongoDB(@PathVariable(value = "userName") String userName) {
		return mongoDBTestService.getOneMongoDB(userName);
	}

	//查询所有数据
	@GetMapping(value = "/find/all")
	public List<TestUser> getAllMongoDB() {
		return mongoDBTestService.getAllMongoDB();
	}

	//模糊查询
	@GetMapping(value = "/find/fuzzy/{userName}")
	public List<TestUser> getFuzzyMongoDB(@PathVariable(value = "userName") String userName) {
		return mongoDBTestService.getFuzzyMongoDB(userName);
	}

	//分页查询
	@GetMapping(value = "/find/page")
	public List<UserDTO> getpageMongoDB() {
		return mongoDBTestService.getpageMongoDB();
	}


	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());					//放入Date类型数据

		System.out.println(calendar.get(Calendar.YEAR));					//获取年份

		System.out.println();					//时（12小时制）

	}

}
